package com.yfp.operation.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yfp.common.security.utils.SecurityUtils;
import com.yfp.operation.domain.AiPresetQuestion;
import com.yfp.operation.mapper.AiPresetQuestionMapper;
import com.yfp.operation.service.AiPresetQuestionService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 * @description 针对表【ai_preset_question(AI模型预设问题表)】的数据库操作Service实现
 * @createDate 2025-02-24 15:08:52
 */
@Service
public class AiPresetQuestionServiceImpl extends ServiceImpl<AiPresetQuestionMapper, AiPresetQuestion> implements AiPresetQuestionService {

    @Resource
    private AiPresetQuestionMapper aiPresetQuestionMapper;

    @Override
    public List<AiPresetQuestion> searchAiPresetQuestion() {
        List<AiPresetQuestion> aiPresetQuestions = aiPresetQuestionMapper.searchAiPresetQuestion();
        return aiPresetQuestions;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveAiPresetQuestion(List<AiPresetQuestion> aiPresetQuestions) {
        // 最多可以添加6条
        int size = CollUtil.size(aiPresetQuestions);
        if (size > 6) {
            throw new RuntimeException("最多可以添加6条");
        }

        DateTime date = DateUtil.date();
        Long userId = SecurityUtils.getUserId();

        for (AiPresetQuestion presetQuestion : aiPresetQuestions) {
            presetQuestion.setCreateTime(date);
            presetQuestion.setCreateUserId(userId);
            presetQuestion.setUpdateTime(date);
            presetQuestion.setUpdateUserId(userId);
        }
        this.remove(Wrappers.lambdaQuery());
        this.saveBatch(aiPresetQuestions);
    }
}




